package com.hankol.feature_wine_meal.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.baselibrary.Constants
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.hankol.feature_wine_meal.R
import com.hankol.feature_wine_meal.bean.CuisineRefreshBean

/**
 * @author: 关忆北、
 * @date: 2022/6/13
 * @Des：头部轮播适配器
 */
class CuisineVpAdapter : PagerAdapter() {
    var titleList: ArrayList<String> = arrayListOf()//title集合
    var listView: ArrayList<View> = arrayListOf()
    var hasMap: HashMap<Int, CuisineRvAdapter> = hashMapOf()
    private var sourceBannerListBean: ArrayList<CuisineRefreshBean> = arrayListOf()
    fun setData(sourceBannerListBean: ArrayList<CuisineRefreshBean>) {
        hasMap.clear()
        this.sourceBannerListBean.clear()
        this.sourceBannerListBean.addAll(sourceBannerListBean)
        notifyDataSetChanged()
    }

    fun setTitles(titleList: ArrayList<String>) {
        this.titleList = titleList
    }

    @SuppressLint("SetTextI18n")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val inflate = LayoutInflater.from(container.context).inflate(R.layout.item_cuisine_rv, container, false)
        val rvCuisine = inflate.findViewById<RecyclerView>(R.id.rv_cuisine)
        val rvAdapter = CuisineRvAdapter()
        rvCuisine.adapter = rvAdapter
        rvAdapter.setOnItemClickListener { _, _, position ->
            val hashMap: java.util.HashMap<String, Any> = hashMapOf()
            hashMap["DRINK_FOOD_PLATE_NAME"] = "特色专区"
            if (titleList.isNotEmpty()) {
                hashMap["CLASS_NAME"] = titleList[position]
            }
            hashMap["RECOMMEND_NAME"] = "特色菜系"
            hashMap[Constants.WINE_MEAL_DETAILS_ID] = rvAdapter.data[position].id
            ARouterUtils.jump(ActivityRouter.WINE_MEAL.WINE_MEAL_DETAILS, hashMap)
        }
        rvAdapter.setNewData(sourceBannerListBean[position].appetizerDiets)
        hasMap[position] = rvAdapter
        container.addView(inflate)
        return inflate
    }

    override fun getCount(): Int {
        return sourceBannerListBean.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
        container.removeView(view as View)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }
}